package com.project.livetv.activity;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.project.livetv.R;
import com.project.livetv.callback.NetCallBack;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by li on 2016/5/9.
 */
public abstract class BaseActivity extends AppCompatActivity implements PlatformActionListener {


    private Callback.Cancelable cancelable;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        initView();
        initData();
        setData();
        setListener();
        ShareSDK.initSDK(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    public abstract void initView();

    public abstract void initData();

    public abstract void setData();

    public abstract void setListener();


    //返回主界面
    public void backFinish(View view) {
        finish();
        overridePendingTransition(R.anim.activity_zoom_out, R.anim.activity_zoom_in);
    }


    /**
     * 下载方法
     */
    protected void downLoadDataInActivity(String url, final NetCallBack netCallBack) {
        RequestParams params = new RequestParams(url);
        if (cancelable != null && cancelable.isCancelled()) {
            cancelable.cancel();
        }
        netCallBack.onStart();
        cancelable = x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                netCallBack.onSucess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                netCallBack.onError(ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

                netCallBack.onCancled();
            }

            @Override
            public void onFinished() {
                netCallBack.onFinished();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (cancelable != null && cancelable.isCancelled()) {
            cancelable.cancel();
        }
    }

    public void load() {
        Platform weibo = ShareSDK.getPlatform(QQ.NAME);
        weibo.setPlatformActionListener(this);
        weibo.authorize();
//移除授权
//weibo.removeAccount(true);
    }

    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
//解析部分用户资料字段
        String id, name, description, profile_image_url;
        id = res.get("id").toString();//ID
        name = res.get("name").toString();//用户名
        description = res.get("description").toString();//描述
        profile_image_url = res.get("profile_image_url").toString();//头像链接
        String str = "ID: " + id + ";\n" +
                "用户名： " + name + ";\n" +
                "描述：" + description + ";\n" +
                "用户头像地址：" + profile_image_url;
        System.out.println("用户资料: " + str);
        //用户资源都保存到res
//通过打印res数据看看有哪些数据是你想要的
        if (action == Platform.ACTION_USER_INFOR) {
            PlatformDb platDB = platform.getDb();//获取数平台数据DB
            //通过DB获取各种数据
            platDB.getToken();
            platDB.getUserGender();
            platDB.getUserIcon();
            platDB.getUserId();
            platDB.getUserName();


        }
    }


    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(Platform platform, int i) {

    }

    public void showShare() {
        // ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }
}
